package AdminController.AdminExe;

import PhotoController.PhotoOldBean;




import org.json.simple.*; 

import common.PagingManager;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

import ApplicationsOld.ApplicationsOldDAO;
import ApplicationsOld.ApplicationsOldBean;
import AdminController.AdminCheck.AdminCheckBean;
import MassageController.Massage.*;
import AdminController.AdminCheck.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.text.html.HTMLDocument.Iterator;



public class AdminExe extends HttpServlet implements Serializable{
	
	private static final long serialVersionUID = 1L;
	private static final String sessionID  = null;
	private HttpSession session = null;
       
   
    public AdminExe() {
        super();
       
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html; charset=UTF-8");
			
			String action_code = request.getParameter("action_code");
			int page = 0;
			int id = 0;
			int up = 0;
			int Fup = 0;
			
			AdminExeDAO aDao = new AdminExeDAO();
			AdminExeBean aeBean = new AdminExeBean();
			AdminExeBean aeBeanO = new AdminExeBean();
			AdminExeBean aeyBean = new AdminExeBean();
			
			AdminExeBean[] aeBeanB = null;
			AdminExeBean[] aeBeanC = null;
			
			AdminCheckBean[] admincheckbean = null;
			AdminCheckDAO admincheckdao = new AdminCheckDAO();
			
			JSONArray inner = new JSONArray();
			JSONObject obj = new JSONObject();
			JSONObject obj2 = new JSONObject();
			
			RequestDispatcher view = null;
			PrintWriter out =response.getWriter();
			
			String nick = "";
			session = request.getSession();
		
			
			
			if(action_code.equals("list")){
				System.out.println("리스트");
				String numMode = request.getParameter("num");
				int start = numMode != null ? Integer.parseInt(numMode) : -1;
				System.out.println("몇번:"+start);
					
				switch(start){
					case 1:
							
							System.out.println("할배/할매 접수리스트");
						
							int currentPage = 1;
					 		if(request.getParameter("currentPage")!=null && !"".equals(request.getParameter("currentPage"))){
								currentPage=Integer.parseInt(request.getParameter("currentPage"));
							}
					 		if(currentPage == 0){
					 			currentPage = 1;
					 		}
					 		HashMap param=new HashMap();
							param = new HashMap<String, Object>();
							param.clear();
							
							int count = aDao.memberCount();
					 		param.put("totalCount",count);
					 		param.put("pageSize", 10);
							param.put("currentPage", currentPage );
							param.put("pageGroupSize", 5 );
							
							PagingManager paging = new common.PagingManager();
							paging.setPageNo(param);
							
							aeBeanB=aDao.getList(currentPage, 10);
							request.setAttribute("list",aeBeanB);
							request.setAttribute("paging", paging.getReturnHtml());
							request.setAttribute("page","/Manage/Man_OldSubmit");
							view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
							view.forward(request, response);
							
							break;
					 
					case 2:
							System.out.println("리스트를 만들어보자꾸나");
							
							aeBean.setMem_Id(Integer.parseInt(request.getParameter("Mem_Id")));
							
							aeBean = aDao.getCheckList(aeBean);
							String aeBeanc = aDao.getDvalueList(aeBean);
							String aeBeand = aDao.getVvalueList(aeBean);
							
							response.setContentType("text/xml");
							
								obj2.put("id",aeBean.getID());
								obj2.put("mem_day",aeBean.getMem_Day());
								obj2.put("mem_id",aeBean.getMem_Id());
								obj2.put("mem_mobile",aeBean.getMem_Mobile());
								obj2.put("mem_month",aeBean.getMem_Month());
								obj2.put("mem_nameh",aeBean.getMem_NameH());
								obj2.put("mem_namek",aeBean.getMem_NameK());
								obj2.put("mem_nick",aeBean.getMem_Nick());
								obj2.put("mem_year",aeBean.getMem_Year());
								obj2.put("members_id",aeBean.getMembers_Id());
								obj2.put("old_add",aeBean.getOld_Add());
								obj2.put("old_gender",aeBean.getOld_Gender());
								obj2.put("old_kind",aeBean.getOld_Kind());
								obj2.put("old_money",aeBean.getOld_Money());
								obj2.put("old_photo",aeBean.getOld_Photo());
								obj2.put("mem_email",aeBean.getMem_Email());
								obj2.put("mem_nick",aeBean.getMem_Nick());
								obj2.put("Dvalue",aeBeanc);
								obj2.put("Vvalue",aeBeand );
								inner.add(obj2);
						
					
					obj.put("conversion",inner);
					System.out.println(obj);
					out.println(obj);
				    out.close();
				    break;
				
					case 3:
						System.out.println("리스트를 삭제해보자꾸나!!!");
						
						id = Integer.parseInt(request.getParameter("id"));
						page = Integer.parseInt(request.getParameter("page"));
						nick = request.getParameter("nick");
						System.out.println("사람이름:"+nick);
						
						currentPage = 1;
				 		if(request.getParameter("currentPage")!=null && !"".equals(request.getParameter("currentPage"))){
							currentPage=Integer.parseInt(request.getParameter("currentPage"));
						}
				 		if(currentPage == 0){
				 			currentPage = 1;
				 		}
				 		param=new HashMap();
						param = new HashMap<String, Object>();
						param.clear();
				 		
				 		param.put("pageSize",10);
						param.put("currentPage", currentPage );
						param.put("pageGroupSize", 5 );
						
						up = 3;
						aDao.checkupO(id,up);
						aDao.aceptDel(id);
						count = aDao.memberCount();
						aeBeanB=aDao.getList(currentPage, 10);
						aDao.finalAceptO(id, nick);
						
						param.put("totalCount",count);
						paging = new common.PagingManager();
						paging.setPageNo(param);
						
						request.setAttribute("list",aeBeanB);
						request.setAttribute("page","/Manage/Man_OldSubmit");
						request.setAttribute("paging", paging.getReturnHtml());
						view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
						view.forward(request, response);
					
						System.out.println("삭제가 되었구나!!!");
						break;
						
					case 4:
						System.out.println("피도 안마른것들 접수리스트");
						
						currentPage = 1;
				 		if(request.getParameter("currentPage")!=null && !"".equals(request.getParameter("currentPage"))){
							currentPage=Integer.parseInt(request.getParameter("currentPage"));
						}
				 		if(currentPage == 0){
				 			currentPage = 1;
				 		}
				 		param=new HashMap();
						param = new HashMap<String, Object>();
						param.clear();
				 		
				 		param.put("pageSize",10);
						param.put("currentPage", currentPage );
						param.put("pageGroupSize", 5 );
						
						count = aDao.memberCountY();
						aeBeanB=aDao.getYList(currentPage, 10);
						param.put("totalCount",count);
						
						paging = new common.PagingManager();
						paging.setPageNo(param);
						
						request.setAttribute("paging", paging.getReturnHtml());
						request.setAttribute("list",aeBeanB);
						request.setAttribute("page","/Manage/Man_YoungSubmit");
						view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
						view.forward(request, response);
						
						break;
						
					case 5:
						System.out.println("피도 안마른것들 리스트만들어보자꾸나");
						aeyBean.setMem_Id(Integer.parseInt(request.getParameter("Mem_Id")));
						System.out.println(Integer.parseInt(request.getParameter("Mem_Id")));
						aeyBean = aDao.getCheckYList(aeyBean);
						
						String aeBeane = aDao.getDvalueYList(aeyBean);
						String aeBeanf = aDao.getVvalueYList(aeyBean);
						String cid =  aDao.checkCid(aeyBean);
						System.out.println(cid);
						response.setContentType("text/xml");
						
						obj2.put("id",aeyBean.getID());
						obj2.put("mem_namek",aeyBean.getMem_NameK());
						obj2.put("mem_nameh",aeyBean.getMem_NameH());
						obj2.put("mem_year",aeyBean.getMem_Year());
						obj2.put("mem_month",aeyBean.getMem_Month());
						obj2.put("mem_day",aeyBean.getMem_Day());
						obj2.put("mem_mobile",aeyBean.getMem_Mobile());
						obj2.put("members_id",aeyBean.getMembers_Id());
						obj2.put("bby_add",aeyBean.getBby_Add());
						obj2.put("bby_kind",aeyBean.getBby_Kind());
						obj2.put("bby_genger",aeyBean.getBby_Gender());
						obj2.put("bby_photo",aeyBean.getBby_Photo());
						obj2.put("bby_contents",aeyBean.getBby_Contents());
						obj2.put("cid",cid);
						obj2.put("dvalue", aeBeane);
						obj2.put("vvalue", aeBeanf);
						obj2.put("memnick",aeyBean.getMem_Nick());
						inner.add(obj2);
						
						obj.put("conversion",inner);
						System.out.println(obj);
						out.println(obj);
					    out.close();
					    break;	
					    
					case 6:
						System.out.println("애새끼들 모아오자");
						aeyBean.setCID(Integer.parseInt(request.getParameter("cid")));
						System.out.println("번호:"+Integer.parseInt(request.getParameter("cid")));
						
						aeyBean = aDao.getChildList(aeyBean);
						
						response.setContentType("text/xml");
						
						obj2.put("id",aeyBean.getID());
						obj2.put("babyplus_id",aeyBean.getBabyplus_Id());
						obj2.put("bbp_namek",aeyBean.getBbp_Namek());
						obj2.put("bbp_nameh",aeyBean.getBbp_NameH());
						obj2.put("bbp_birth",aeyBean.getBbp_Birth());
						obj2.put("bbp_gender",aeyBean.getBbp_Gender());
						obj2.put("bbp_photo",aeyBean.getBbp_Photo());
						
						inner.add(obj2);
						
						obj.put("conversion",inner);
						System.out.println(obj);
						out.println(obj);
					    out.close();
					    break;	
					case 7:
						System.out.println("애새끼들 리스트삭제해보자꾸나");
						
						id = Integer.parseInt(request.getParameter("id"));
						nick = request.getParameter("nick");						
						
						currentPage = 1;
				 		if(request.getParameter("currentPage")!=null && !"".equals(request.getParameter("currentPage"))){
							currentPage=Integer.parseInt(request.getParameter("currentPage"));
						}
				 		if(currentPage == 0){
				 			currentPage = 1;
				 		}
				 		param=new HashMap();
						param = new HashMap<String, Object>();
						param.clear();
				 		
				 		param.put("pageSize",10);
						param.put("currentPage", currentPage );
						param.put("pageGroupSize", 5 );
						
						up = 3;
						aDao.finalAceptY(id, nick);
						aDao.checkupY(id,up);
						aDao.aceptDelY(id);
						count = aDao.memberCountY();
						aeBeanB=aDao.getYList(currentPage, 10);
						
						
						param.put("totalCount",count);
						
						paging = new common.PagingManager();
						paging.setPageNo(param);
						
						request.setAttribute("list",aeBeanB);
						request.setAttribute("paging", paging.getReturnHtml());
						request.setAttribute("page","/Manage/Man_YoungSubmit");
						view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
						view.forward(request, response);
					
						System.out.println("삭제가 되었구나!!!");
						break;
				}
			}else if(action_code.equals("FinalAcept")){
				System.out.println("최종연결");
				String numMode = request.getParameter("num");
				int start = numMode != null ? Integer.parseInt(numMode) : -1;
				System.out.println("몇번:"+start);
				
				switch(start){
				case 1:
					System.out.println("페이지이동");
					
					int currentPage = 1;
			 		if(request.getParameter("currentPage")!=null && !"".equals(request.getParameter("currentPage"))){
						currentPage=Integer.parseInt(request.getParameter("currentPage"));
					}
			 		if(currentPage == 0){
			 			currentPage = 1;
			 		}
					
			 		HashMap param=new HashMap();
					param = new HashMap<String, Object>();
					param.clear();
						
					param.put("pageSize", 10);
					param.put("currentPage", currentPage );
					param.put("pageGroupSize", 5 );
					
					
					aeBeanB=aDao.finalList(currentPage,10);
					aeBeanC=aDao.RefinalList(currentPage,10); //맵핑된후 노인측 수락대기중..
					int count = aDao.finalCount();
					param.put("totalCount", count);
					
					PagingManager paging = new common.PagingManager();
					paging.setPageNo(param);
					
					int mappingcount = aDao.mappingCount();
					
					request.setAttribute("paging", paging.getReturnHtml());
					request.setAttribute("list",aeBeanB);
					request.setAttribute("Rlist",aeBeanC);
					request.setAttribute("mappingcount",mappingcount);
					request.setAttribute("page","/Manage/Man_Check");
					view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
					view.forward(request, response);
					break;
				case 2:
					System.out.println("해당부모 조건긁어오기");
					aeBean.setMem_Id(Integer.parseInt(request.getParameter("mid")));
					
					String Mem_Nick = aDao.checkNick(Integer.parseInt(request.getParameter("mid")));
					aeBean = aDao.finalYoungList(aeBean);
					aeBean = aDao.finalPhoneList(aeBean);
					String aeBeanc = aDao.getDvalueList(aeBean);
					String aeBeand = aDao.getVvalueList(aeBean);
					
					response.setContentType("text/xml");
					
					obj2.put("bby_gender",aeBean.getBby_Gender());
					obj2.put("bby_kind",aeBean.getBby_Kind());
					obj2.put("bby_money",aeBean.getBby_Money());
					obj2.put("bby_photo",aeBean.getBby_Photo());
					obj2.put("bby_add",aeBean.getBby_Add());
					obj2.put("members_id",aeBean.getMembers_Id());
					obj2.put("members_nick",Mem_Nick);
					obj2.put("dvalue",aeBeanc);
					obj2.put("vvalue",aeBeand);
					obj2.put("mem_mobile",aeBean.getMem_Mobile());
					obj2.put("mem_phone",aeBean.getMem_Phone());
					obj2.put("bby_check",aeBean.getBby_Check());
					inner.add(obj2);
					obj.put("conversion",inner);
					System.out.println(obj);
					out.println(obj);
				    out.close();
				    break;
				
				
				case 3:
					
					System.out.println("맵핑시키자!!!");
					String city = "";
					HashMap<Integer,String> result;
					ArrayList<AdminExeBean> list;
					java.util.Iterator<AdminExeBean> iter;
					
					System.out.println("부모(문자)아이디:"+request.getParameter("Mem_Nick"));
					System.out.println("희망성별:"+request.getParameter("bby_gender"));
					System.out.println("부모주소:"+request.getParameter("bby_add"));
					System.out.println("희망보육종류:"+request.getParameter("bby_kind"));
					System.out.println("희망시간:"+request.getParameter("bby_money"));
					System.out.println("부모(숫자)아이디:"+request.getParameter("memid"));
					
					StringTokenizer st = new StringTokenizer(request.getParameter("bby_add"), " "); 
					
					for(int x = 1 ; st.hasMoreElements() ; x++){
						  
						System.out.println("토큰: "+x+":"+st.nextToken()); //구만 뽑아내기!!
							if(x == 1){
								 city = st.nextToken();
							}
						}
					System.out.println("원하는구역:"+city);
					int vsum=aDao.finalVvalue(Integer.parseInt(request.getParameter("memid")));
					int dsum=aDao.finalDvalue(Integer.parseInt(request.getParameter("memid")));
					
					aeBean.setBby_Gender(Integer.parseInt(request.getParameter("bby_gender")));
					aeBean.setBby_Kind(request.getParameter("bby_kind"));
					aeBean.setBby_Money(request.getParameter("bby_money"));
					
				System.out.println("******제1지망 할아버지/할머니 맵핑(조건100%맞는분!!)******");
					result=aDao.firstMap(aeBean,city,vsum,dsum);
					
				System.out.println("******제2지망 할아버지/할머니 맵핑(자격증 요일 협상가능!!)******");
					list = aDao.secondMap(aeBean, city, vsum, dsum);
					
					iter = list.iterator();
					
					System.out.println(result.size());
					System.out.println(list.size());
					
					AdminExeBean temp;
					while(iter.hasNext()){
						temp = iter.next();
						
						if(result.get(temp.getMem_Id()) != null){ //1지망과 중복되는 아이디 삭제!!!
								System.out.println("삭제아이디"+temp.getMem_Id());
								System.out.println("삭제아이디(문자)"+temp.getMem_Nick());
								iter.remove();
							}
					}
					
					request.setAttribute("first",result);
					request.setAttribute("second",list);
					request.setAttribute("page","/Manage/Man_Check");
					view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
					view.forward(request, response);
					break;
				
				case 4:
					System.out.println("어느 새끼랑 연결되어있나??");
					aeBean.setMem_Id(Integer.parseInt(request.getParameter("mid")));//젊은이 번호
					
					ApplicationsOldDAO aOld = new ApplicationsOldDAO();
					aeBean.setRmem_Id(aOld.mapOld(Integer.parseInt(request.getParameter("mid"))));//할아버지 맵핑번호
					
					aeBeanO = aOld.finalOld(aeBean); //할아버지 정보 가져오기..
					aeBean = aOld.finalYoung(aeBean);
					
					response.setContentType("text/xml");
					
					obj2.put("mem_namek",aeBean.getMem_NameK());
					obj2.put("mem_add1",aeBean.getMem_Address1());
					obj2.put("mem_add2",aeBean.getMem_Address2());
					obj2.put("mem_phone",aeBean.getMem_Phone());
					obj2.put("mem_mobile",aeBean.getMem_Mobile());
					obj2.put("bby_photo",aeBean.getBby_Photo());
					obj2.put("mem_email",aeBean.getMem_Email());
					
					obj2.put("mem_namekO",aeBeanO.getMem_NameKo());
					obj2.put("mem_add1O",aeBeanO.getMem_Address1O());
					obj2.put("mem_add2O",aeBeanO.getMem_Address2O());
					obj2.put("mem_phoneO",aeBeanO.getMem_PhoneO());
					obj2.put("mem_mobileO",aeBeanO.getMem_MoblieO());
					obj2.put("old_photo",aeBeanO.getOld_Photo());
					obj2.put("mem_emailO",aeBeanO.getMem_EmailO());
					
					inner.add(obj2);
					obj.put("conversion",inner);
					System.out.println(obj);
					out.println(obj);
				    out.close();
				    break;
					
					
				}
			}else if(action_code.equals("mapping")){
				System.out.println("맵핑처리 하자꾸나!!");
				

				MassageDAO massageDao = new MassageDAO();
				MassageBean massageDeep = new MassageBean();
				
				
				int Mem_Id = Integer.parseInt(request.getParameter("Mem_Id"));
				int first_Id = Integer.parseInt(request.getParameter("first_Id"));
				
				up = 4;
				Fup = 2;
				int mFup = 0;
				
				aDao.checkupO(Mem_Id, up);  //매핑완료후 할배 old_check변경(+4)
				aDao.finalupO(Mem_Id, Fup); //Mem_Check변경 (+2)(할아버지)
				aDao.finalupY(first_Id, Fup , mFup); //Mem_Check변경 (+2)(젊은이) Map_Check (+0)변경
				
				aDao.mapping(Mem_Id, first_Id); //최종 맵핑된 노인/젊은이 등록
				
				String oldNick = aDao.checkNick(Mem_Id);
				String youngNick = aDao.checkNick(first_Id);
				
				massageDeep.setMsgTo(oldNick);
				massageDeep.setMsgForm("管理者");
				massageDeep.setMsgContents(youngNick+"様と繋がれました。求人申請で承認をください！");
				
				massageDao.Write(massageDeep);
				
				out.println( "<script>" );
				out.println( "location.href=\"/KOMORIUTA/AdminExe.do?action_code=FinalAcept&num=1&page=1\";" );
				out.println( "</script>" );
			
			}
		}
	}